﻿
var vm = new Vue({
    el: '#app',
    data: function () {
        return {
            pagename: "生产报工",
            common_fn: null,
            userinfo: null,
            loading: false,
            maxScreenHeight: 0,
            mainTable_maxHeight: 0,
            orderData: [],//订单
            selectedOrderID: "",
            selectedOrderNo: "",
            selectedProductioScheduleID: "",//选中的订单对应的批次计划ID
            reportsheets: [],  //开工列表
            reportsheetsLength: 0,//开工列表长度
            multipleSelection: [],
            reportworkLoading: false,
            report_index: 0,//选中开工条目index
            report_row: "",//选中开工条目row
            reportworkDialog: false,//开工对话框
        }
    },
    methods: {
        //看图
        retColor: function (item) {
            if (item.column.label == '图号' || item.column.label == '批次号') { return 'viewDrawing' }
        },
        cell_click: function (row, column, cell) {
            var self = this;
            if (column.label == '图号') {
                //
            }
        },
        //多选
        handleSelectionChange: function (val) {
            console.log(val)
        },
        // 获取所有订单
        getOrders: function () {
            var self = this;
            self.loading = true;
            var filters = [{
                "classFlag": 1,
                "classType": "MesOrderSheet",
                "propName": "state",
                "op": 4,
                "propValue": "2",      //已排产发布的订单
                "propValue2": "",
                "logic": 0,
                "leftSplitter": 0,
                "rightSplitter": 0,
                "bAddWhereClauseFlag": true
            }];
            self.common_fn.queryObject("MesOrderSheet", filters, self.userinfo, function (response) {
                if (response.resFlag == 0) {
                    for (i = response.result.length - 1; i >= 0; i--) {
                        response.result[i].properties.state == 0 ? response.result[i].properties['state_name'] = '待审核' : response.result[i].properties.state == 1 ? response.result[i].properties['state_name'] = '审核中' : response.result[i].properties['state_name'] = '已发布';
                        response.result[i].properties.deliverydate = response.result[i].properties.deliverydate.split(' ')[0];
                        response.result[i].properties.creationdate = response.result[i].properties.creationdate.split(' ')[0]; //APP加
                        response.result[i].properties.orderquantity = Number(response.result[i].properties.orderquantity);
                        self.orderData.push({
                            "id": response.result[i].id,
                            "name": response.result[i].properties.name,
                        })
                    }
                    self.orderData = response.result;
                    if (self.orderData.length !== 0) {
                        self.selectedOrderNo = self.orderData[self.orderData.length - 1].properties.orderno;
                        self.selectedOrderID = self.orderData[self.orderData.length - 1].id
                        self.getProductioSchedule(self.selectedOrderID);
                    }
                } else {
                    notify(response.errMsg, 'error')
                }
                self.loading = false;
            })
        },
        //获取批次计划ID
        getProductioSchedule: function (selectedOrderID) {
            var self = this;
            self.loading = true;
            var param = {
                "appName": "mes",
                "sql": "select * from MES_ORDERSHEETPRODUCT where A_OBJID = " + selectedOrderID
            };
            self.common_fn.sqlFn(param, function (response) {
                if (response.resFlag == 0) {
                    self.selectedProductioScheduleID = parseInt(response.result[0].properties.b_objid)
                    self.getReportSheets(self.selectedProductioScheduleID)
                } else {
                    notify(response.errMsg, 'error')
                }
            })
        },

        //获取已派工到班组的派工单，准备派工到个人
        getReportSheets: function (selectedProductioScheduleID) {
            var self = this;
            self.loading = true;
            var param = {
                "filters": [{
                    "classFlag": 1,
                    "classType": "MesReportsheet",
                    "propName": "state",
                    "op": 4,
                    "propValue": 2,
                    "propValue2": "",
                    "logic": 0,
                    "leftSplitter": 0,
                    "rightSplitter": 0,
                    "bAddWhereClauseFlag": true
                }, {
                    "classFlag": 1,
                    "classType": "MesReportsheet",
                    "propName": "PERSON_OBJID",
                    "op": 4,
                    "propValue": self.userinfo.user.id,
                    "propValue2": "",
                    "logic": 0,
                    "leftSplitter": 0,
                    "rightSplitter": 0,
                    "bAddWhereClauseFlag": true
                }, {
                    "classFlag": 1,
                    "classType": "MesReportsheet",
                    "propName": "PRODUCT_OBJID",
                    "op": 4,
                    "propValue": selectedProductioScheduleID + '',
                    "propValue2": "",
                    "logic": 0,
                    "leftSplitter": 0,
                    "rightSplitter": 0,
                    "bAddWhereClauseFlag": true
                }],
                "orderItems": [],
                "queryclasses": ["MesReportSheet", "MesProductionSchedule", "PlmPart", "QcpEquipment", "SysPerson", "QcpProcess", "MesSemiFinished", "SysSnapshot"],
                "queryFields": [{
                    "classType": "MesPartSemiFinished",
                    "propName": "IndexNo"
                }],
                "isQueryPreNext": true,
                "isQueryNextConsiderParent": true,
                "user": self.userinfo.user
            }
            self.common_fn.invoke('JridgeMesSTD_AdditionalService.dll', "JridgeMesSTD_AdditionalService.ReportSheetMgr", "QueryReportSheetToReport", param, self.userinfo, function (response) {
                if (response.resFlag == 0) {
                    self.reportsheetsLength = response.result.items.length;
                    for (var i = 0; i < response.result.items.length; i++) {
                        response.result.items[i]['properties'] = {};
                        for (var k = 0; k < response.result.items[i].itemObjects.length; k++) {
                            response.result.items[i].properties[response.result.items[i].itemObjects[k].type] = response.result.items[i].itemObjects[k].properties;
                            response.result.items[i].properties[response.result.items[i].itemObjects[k].type]['id'] = response.result.items[i].itemObjects[k].id;
                        }
                    }
                    reportsheets_temp = [];
                    for (var i = 0; i < response.result.items.length; i++) {
                        reportsheets_temp.push({
                            "mesreportsheet_objid": response.result.items[i].properties.mesreportsheet.id,
                            "productlayer": response.result.items[i].properties.messemifinished.productlayer,  //生产层
                            "productname": response.result.items[i].properties.mesproductionschedule.name,
                            "equipment": response.result.items[i].properties.qcpequipment.name,
                            "materialno": response.result.items[i].properties.plmpart.materialno,
                            "identityno": response.result.items[i].properties.plmpart.identityno,
                            "plmpartname": response.result.items[i].properties.plmpart.name,  //零件名称
                            "processno": response.result.items[i].properties.qcpprocess.processno,  //工序号
                            "qcpname": response.result.items[i].properties.qcpprocess.name,//工序名称
                            "assigndate": response.result.items[i].properties.mesreportsheet.creationdate.substring(0, 10), //派工时间
                            "startdate": response.result.items[i].properties.messemifinished.startdate,
                            "enddate": response.result.items[i].properties.messemifinished.enddate,
                            "assignquantity": response.result.items[i].properties.mesreportsheet.assignquantity,
                        })
                    }
                    //排序
                    for (var i = 0; i < reportsheets_temp.length; i++) {
                        for (var j = 0; j < reportsheets_temp.length - 1 - i; j++) {
                            if (reportsheets_temp[j].productlayer < reportsheets_temp[j + 1].productlayer) {
                                var temp = reportsheets_temp[j + 1];
                                reportsheets_temp[j + 1] = reportsheets_temp[j];
                                reportsheets_temp[j] = temp;
                            }
                        }
                    }
                    self.reportsheets = reportsheets_temp;
                }
                self.loading = false;
            })
        },

        //选择某个订单
        selectOrder: function () {
            this.getProductioSchedule(this.selectedOrderID)
        },


        //报工
        report: function (index, row) {
            // var reportnsheets_productlayers = [];
            // for (var i = 0; i < this.reportsheets.length; i++) {
            //     if (i !== index) {
            //         reportnsheets_productlayers.push(this.reportsheets[i].productlayer)
            //     }
            // }
            //前后道工序判断
            // if (row.productlayer >= Math.max.apply(null, reportnsheets_productlayers)) {
            if (index == 0) {
                this.reportworkDialog = true;
                this.report_index = index;
                this.report_row = row;
            } else {
                message('前道工序未完成，请按照顺序依次生产！', 'error');
            }
        },


        //报工确认
        reportConfirm: function () {
            var self = this;
            self.reportworkLoading = true;
            self.common_fn.getCurrentTime(function (time) {
                var currentTime = time;
                var reportsheet = {
                    id: self.report_row.mesreportsheet_objid,
                    type: "MesReportSheet",
                    properties: {
                        "person_objid": self.userinfo.user.id,
                        "realquantity": self.report_row.assignquantity,//实际完工数量
                        "reporttime": currentTime,
                        "reportor": self.userinfo.user.properties.name//报工人
                    }
                };
                var param = {
                    "appName": "MES",
                    "connectionId": 1,
                    "factory": self.common_fn.configInfo.factory,
                    "user": self.userinfo.user
                };
                param['reportSheets'] = [reportsheet];
                self.common_fn.invoke('JridgeMesSTD_AdditionalService.dll', "JridgeMesSTD_AdditionalService.ReportSheetMgr", 'ReportWorkDirectly', param, self.userinfo, function (response) {
                    if (response.resFlag == 0) {
                        self.reportworkDialog = false;
                        self.reportsheets.splice(self.report_index, 1);
                        self.reportsheetsLength = self.reportsheetsLength - 1;
                        if (self.reportsheetsLength == 0) {
                            //state 置为3 ，该订单已全部报工
                            var param = {
                                "appName": "mes",
                                "sql": "update MES_OrderSheet set state = 3 where objid = " + self.selectedOrderID
                            }
                            self.common_fn.sqlFn(param, function (response) {
                                if (response.resFlag == 0) {
                                    message('该订单已完成全部报工！', 'success');
                                } else {
                                    notify(response.errMsg, 'error')
                                }
                            })
                        } else {
                            message('报工成功', 'success');
                        }
                    } else {
                        notify(response.errMsg, 'error');
                    }
                    self.reportworkLoading = false;
                })
            })
        }

    },


    mounted: function () {
        var self = this;
        self.common_fn = new common_fn();
        self.common_fn_school = new common_fn_school();
        self.common_fn.init(function () {
            self.loading = true;
            self.common_fn.UserInfo(function (response) {
                if (response.user == null) {
                    window.location.href = 'login.html?redirect=claimWork.html';
                }
                self.userinfo = response;
                self.common_fn.CheckFunction(self.userinfo, 'mes', 'mes', 'WorkShopPlanMgr', '', '', 'workshopplan', function () { })
                self.getOrders();
                self.loading = false;
            })
        });
    }
})

window.onload = function () {
    if (vm.mainTable_maxHeight != null || vm.mainTable_maxHeight != undefined) {
        window.setTimeout(function () {
            vm.mainTable_maxHeight = $(window).height() - 150;
        }, 100)
    }
}
